package com.iris.thread.produceConsume;

import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;


/**
 * 项目: thinking
 * 包名: com.iris.thread.produceConsume
 * 作者: victor
 * 日期: 2016年10月30日
 * 说明: BlockQueueTest
 */
public class BlockQueueTest {
	public static void main(String[] args) {
		BlockingQueue<String> blocks= new LinkedBlockingQueue<String>(1000);
//		BlockingQueue<String> blocks1= new ArrayBlockingQueue<String>(1000);
		ExecutorService es = Executors.newFixedThreadPool(2);
		es.execute(new BlockQueueConsume(blocks));
		es.execute(new BlockQueueProduce(blocks));
	}
}

class BlockQueueProduce implements Runnable{

	private boolean startFlag=true;
	
	public boolean isStartFlag() {
		return startFlag;
	}
	public void setStartFlag(boolean startFlag) {
		this.startFlag = startFlag;
	}
	private BlockingQueue<String> blocks=null;

	public BlockQueueProduce(BlockingQueue<String> blocks){
		this.blocks = blocks;
	}
	public void run() {
		String uuid="";
		while(startFlag){
			try {
//				if(blocks.size()>1000){
//					synchronized (blocks) {
//						blocks.wait();
//					}
//				}
				uuid= UUID.randomUUID().toString();
				blocks.put(uuid);
				System.out.println(uuid+"======================="+blocks.size()+"===="+System.currentTimeMillis());
				
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
}



class BlockQueueConsume implements Runnable{

	private boolean startFlag=true;
	
	public boolean isStartFlag() {
		return startFlag;
	}
	public void setStartFlag(boolean startFlag) {
		this.startFlag = startFlag;
	}
	private BlockingQueue<String> blocks=null;

	public BlockQueueConsume(BlockingQueue<String> blocks){
		this.blocks = blocks;
	}
	public void run() {
		String uuid="";
		while(startFlag){
			try {
				uuid = blocks.take();
				System.out.println(uuid+"====="+blocks.size()+"===="+System.currentTimeMillis());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
}
